﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CountVisistor();
            FillHomePage();
        }
    }
    void CountVisistor()
    {
        lblVisistorCount.Text = Application["visistor"].ToString();
        lblTime.Text = DateTime.Now.ToLongDateString();
        lblTotalNews.Text = DatabaseInteraction.CountNews().ToString();
    }
    protected void ddlChanel_SelectedIndexChanged(object sender, EventArgs e)
    {
        int index = ddlChanel.SelectedIndex;
        switch (index)
        {
            case 0:
                mvwChannel.ActiveViewIndex = -1;
                mvwNewsContent.SetActiveView(vwHomePage);
                break;
            case 1:
                mvwChannel.SetActiveView(vwChannelWorld);
                mvwNewsContent.SetActiveView(vwWorldPage);
                break;
            case 2:
                mvwChannel.SetActiveView(vwChannelEntertainment);
                mvwNewsContent.SetActiveView(vwEntertainmentPage);
                break;
            case 3:
                mvwChannel.SetActiveView(vwChannelSport);
                mvwNewsContent.SetActiveView(vwSportPage);
                break;
        }
    }

    protected void btnCreateNews_Click(object sender, EventArgs e)
    {
        Response.Redirect("CreateNews.aspx");
    }
    //Fill Du lieu vao view Home Page
    void FillHomePage()
    {
        List<New_Report> worlds = DatabaseInteraction.GetAllNewsForHomePage("World");
        List<New_Report> entertainments = DatabaseInteraction.GetAllNewsForHomePage("Entertainment");
        List<New_Report> sports = DatabaseInteraction.GetAllNewsForHomePage("Sport");

        //World Control
        WorldControl.LblTitleChanel.Text = "World News";
        WorldControl.LblTitleNew.Text = worlds[0].TitleNews;
        if (worlds[0].NewsContent.Length > 400)
        {
            WorldControl.LblNewsContent.Text = worlds[0].NewsContent.Substring(0, 399);
        }
        else
        {
            WorldControl.LblNewsContent.Text = worlds[0].NewsContent;
        }
        WorldControl.LblTimeNewsPost.Text = worlds[0].TimePost.ToString();
        WorldControl.LblNewsAuthor.Text = worlds[0].Author;
        WorldControl.LbtnTotalComments.Text = worlds[0].CountComments + " comments";
        WorldControl.LbtnTitleNews2.Text = worlds[1].TitleNews;
        WorldControl.LbtnTitleNews3.Text = worlds[2].TitleNews;
        WorldControl.LbtnTitleNews4.Text = worlds[3].TitleNews;
        WorldControl.LblNews2ID.Text = worlds[1].NewsID.ToString();
        WorldControl.LblNews3ID.Text = worlds[2].NewsID.ToString();
        WorldControl.LblNews4ID.Text = worlds[3].NewsID.ToString();
        WorldControl.LblNewsID.Text = worlds[0].NewsID.ToString();

        //Entertainment Control
        EntertainmentControl.LblTitleChanel.Text = "Entertainment News";
        EntertainmentControl.LblTitleNew.Text = entertainments[0].TitleNews;
        if (entertainments[0].NewsContent.Length > 400)
        {
            EntertainmentControl.LblNewsContent.Text = entertainments[0].NewsContent.Substring(0, 399);
        }
        else
        {
            EntertainmentControl.LblNewsContent.Text = entertainments[0].NewsContent;
        }
        EntertainmentControl.LblTimeNewsPost.Text = entertainments[0].TimePost.ToString();
        EntertainmentControl.LblNewsAuthor.Text = entertainments[0].Author;
        EntertainmentControl.LbtnTotalComments.Text = entertainments[0].CountComments + " comments";
        EntertainmentControl.LbtnTitleNews2.Text = entertainments[1].TitleNews;
        EntertainmentControl.LbtnTitleNews3.Text = entertainments[2].TitleNews;
        EntertainmentControl.LbtnTitleNews4.Text = entertainments[3].TitleNews;
        EntertainmentControl.LblNews2ID.Text = entertainments[1].NewsID.ToString();
        EntertainmentControl.LblNews3ID.Text = entertainments[2].NewsID.ToString();
        EntertainmentControl.LblNews4ID.Text = entertainments[3].NewsID.ToString();
        EntertainmentControl.LblNewsID.Text = entertainments[0].NewsID.ToString();

        //Sport Control
        SportControl.LblTitleChanel.Text = "Sport News";
        SportControl.LblTitleNew.Text = sports[0].TitleNews;
        if (sports[0].NewsContent.Length > 400)
        {
            SportControl.LblNewsContent.Text = sports[0].NewsContent.Substring(0, 399);
        }
        else
        {
            SportControl.LblNewsContent.Text = sports[0].NewsContent;
        }
        SportControl.LblTimeNewsPost.Text = sports[0].TimePost.ToString();
        SportControl.LblNewsAuthor.Text = sports[0].Author;
        SportControl.LbtnTotalComments.Text = sports[0].CountComments + " comments";
        SportControl.LbtnTitleNews2.Text = sports[1].TitleNews;
        SportControl.LbtnTitleNews3.Text = sports[2].TitleNews;
        SportControl.LbtnTitleNews4.Text = sports[3].TitleNews;
        SportControl.LblNews2ID.Text = sports[1].NewsID.ToString();
        SportControl.LblNews3ID.Text = sports[2].NewsID.ToString();
        SportControl.LblNews4ID.Text = sports[3].NewsID.ToString();
        SportControl.LblNewsID.Text = sports[0].NewsID.ToString();

    }

    void ReadNewDetail(int newID)
    {
        string content = DatabaseInteraction.GetNewContentByID(newID);
        lblNewWorldContent.Text = content;
    }
    void GridViewWorldSelected(int index)
    {
        int newID = Convert.ToInt32(gvwWorldPage.Rows[index].Cells[1].Text);
        lblWorldTitle.Text = gvwWorldPage.Rows[index].Cells[3].Text;
        ReadNewDetail(newID);
        ShowComments(newID);
        DatabaseInteraction.UpdateCountViews(newID);
    }
    void ShowComments(int newID)
    {
        pnlWorldComments.Controls.Clear();
        List<Comment> list = DatabaseInteraction.GetComment(newID);
        if (list.Count > 0)
        {
            foreach (Comment c in list)
            {
                MyControls_UserComments comment = LoadControl("~/MyControls/UserComments.ascx") as MyControls_UserComments;
                comment.Comment.Text = c.CommentContent;
                comment.Author.Text = c.Author;
                comment.Time.Text = c.TimePost.ToString();
                pnlWorldComments.Controls.Add(comment);
            }
        }

    }
    protected void gvwWorldPage_SelectedIndexChanged(object sender, EventArgs e)
    {
        int index = gvwWorldPage.SelectedIndex;
        if (index > -1)
        {
            GridViewWorldSelected(index);
        }
    }

    protected void lbtnMostViewWorld_Click(object sender, EventArgs e)
    {
        mvwNewsContent.SetActiveView(vwSearchpage);
        gvwSearch.DataSourceID = "SqlDataSource5";
        gvwSearch.DataBind();
    }

    protected void lbtnLasterWorld_Click(object sender, EventArgs e)
    {
        mvwNewsContent.SetActiveView(vwSearchpage);
        gvwSearch.DataSourceID = "SqlDataSource4";
        gvwSearch.DataBind();
    }

    protected void lbtnMostCommentWorld_Click(object sender, EventArgs e)
    {
        mvwNewsContent.SetActiveView(vwSearchpage);
        gvwSearch.DataSourceID = "SqlDataSource6";
        gvwSearch.DataBind();
    }
    protected void btnSearchWorld_Click(object sender, EventArgs e)
    {
        if (rbtnlWorld.SelectedValue == "Author")
        {
            mvwNewsContent.SetActiveView(vwSearchpage);
            gvwSearch.DataSourceID = "SqlDataSource7";
            gvwSearch.DataBind();
        }
        else if (rbtnlWorld.SelectedValue == "Title")
        {
            mvwNewsContent.SetActiveView(vwSearchpage);
            gvwSearch.DataSourceID = "SqlDataSource8";
            gvwSearch.DataBind();
        }
    }
    //void SearchWorldByAuthor(string name)
    //{        
    //    NewReportDataContext data = new NewReportDataContext();
    //    var query = from s in data.New_Reports
    //                where s.Author.StartsWith(name)
    //                select new
    //                {
    //                    ID = s.NewsID,
    //                    Chanel = s.ChanelName,
    //                    Title = s.TitleNews,
    //                    Author = s.Author,
    //                    TimePost = s.TimePost,
    //                    Views = s.CountRead,
    //                    Comments = s.CountComments
    //                };
    //    gvwSearch.DataSource = query;
    //    gvwSearch.DataBind();
    //}
    protected void lbtnWorldShare_Click(object sender, EventArgs e)
    {
        int index = gvwWorldPage.SelectedIndex;
        int newID = Convert.ToInt32(gvwWorldPage.Rows[index].Cells[1].Text);
        string url = "New_Report.aspx?newsID=" + newID;
        Response.Redirect(url);
    }
}